using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
using SJVars;
using System;
using System.Reflection;
using System.Windows.Forms;
using yW5Lib;
using yWriter6.My;

namespace yWriter6
{
	[StandardModule]
	internal sealed class modImportOutlineToScenes
	{
		private static string HelpText = "A suitable file will consist of separate paragraphs, one per scene.\r\nEach paragraph is a scene description. The first sentence will be used as the short description.\r\nAfter importing, there will be one new chapter for every three scenes.";

		public static bool ImportOutlineToScenes(ref clsProject cProject, bool V6File)
		{
			bool result = false;
			string file = clsShared.cFileSel.GetFile(MyProject.Computer.FileSystem.SpecialDirectories.MyDocuments, "*.txt;*.rtf");
			if (Operators.CompareString(file, "", TextCompare: false) != 0 && cProject.CreateEmptyProject(V6File))
			{
				cProject.Title = "Imported Project";
				cProject.AuthorName = "";
				string TextString = "";
				clsShared.cFile.ReadTextFile(file, ref TextString);
				try
				{
					if (Operators.CompareString(clsShared.cFile.ExtOnly(file).ToLower(), "rtf", TextCompare: false) == 0)
					{
						RichTextBox richTextBox = new RichTextBox();
						try
						{
							richTextBox.Rtf = TextString;
						}
						catch (Exception ex)
						{
							ProjectData.SetProjectError(ex);
							Exception ex2 = ex;
							richTextBox.Text = TextString;
							ProjectData.ClearProjectError();
						}
						TextString = richTextBox.Text;
						TextString = TextString.Replace("\r\n", "\r\n");
						TextString = TextString.Replace("\n", "\r\n");
					}
					clsShared.cHtml.HTMLToPlainText(ref TextString);
					int num = 0;
					string[] array = Strings.Split(TextString, "\r\n");
					clsCharacter cChr = new clsCharacter();
					cChr.Title = "Unnamed";
					cProject.AddChar(ref cChr);
					int num2 = Information.UBound(array);
					int iD = default(int);
					for (int i = 0; i <= num2; i++)
					{
						if (Operators.CompareString(Strings.Trim(array[i]), "", TextCompare: false) != 0)
						{
							int num3 = Strings.InStr(array[i], ".");
							if ((Strings.InStr(array[i], "!") < num3) & (Strings.InStr(array[i], "!") != 0))
							{
								num3 = Strings.InStr(array[i], "!");
							}
							string title = (num3 == 0) ? "No short description" : Strings.Left(array[i], num3);
							if (i % 3 == 0)
							{
								num++;
								clsChapter cChapter = new clsChapter();
								cChapter.Title = "Chapter " + Conversions.ToString(num);
								cChapter.DescText = "";
								cProject.AddChapter(ref cChapter);
								iD = cChapter.ID;
							}
							clsScene Scene = new clsScene();
							Scene.V6 = cProject.V6File;
							Scene.TextRTF = "";
							Scene.Title = title;
							Scene.DescText = array[i];
							Scene.Goal = "";
							Scene.Conflict = "";
							Scene.Outcome = "";
							Scene.SetViewpointByID(1);
							cProject.AddSceneToProjectAndChapter(ref Scene, iD);
						}
					}
					cProject.CriticalSave();
					result = true;
					return result;
				}
				catch (Exception ex3)
				{
					ProjectData.SetProjectError(ex3);
					Exception ex4 = ex3;
					clsShared.cLog.AddAction(ex4, MethodBase.GetCurrentMethod());
					ProjectData.ClearProjectError();
					return result;
				}
			}
			return result;
		}
	}
}
